//
//  LDSCollectionViewModel.m
//  LDSMVVMBase
//
//  Created by kai_leedarson on 2017/10/9.
//  Copyright © 2017年 maple_leedarson. All rights reserved.
//

#import "LDSCollectionViewModel.h"


@interface LDSCollectionViewModel ()
@property (nonatomic, strong) NSMutableArray *viewModels;
@property (nonatomic, strong) NSMutableArray *sectionViewModels;
@end

@implementation LDSCollectionViewModel
- (NSMutableArray *)sectionViewModels {
    if (_sectionViewModels == nil) {
        _sectionViewModels = [NSMutableArray arrayWithCapacity:0];
    }
    return _sectionViewModels;
}

- (NSMutableArray *)viewModels {
    if (_viewModels == nil) {
        _viewModels = [NSMutableArray array];
    }
    return _viewModels;
}


- (void)handleWithCollection:(UICollectionView *)collectionView {
    if (collectionView == nil) {
        NSLog(@"无 collectionView");
        return;
    }
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.pagingEnabled = YES;
    collectionView.delegate = (id<UICollectionViewDelegate>)self;
    collectionView.dataSource = (id<UICollectionViewDataSource>)self;
    collectionView.translatesAutoresizingMaskIntoConstraints = NO;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
}

- (void)handleWithCollectionLayout:(UICollectionViewFlowLayout *)collectionLayout {
    if (collectionLayout == nil) {
        NSLog(@"无 collectionLayout");
        return;
    }
    [collectionLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [collectionLayout setMinimumInteritemSpacing:0.0f];
    [collectionLayout setMinimumLineSpacing:12.0f];
}

- (void)handleWithDataSource:(NSMutableArray *)dataSource {
    self.viewModels = dataSource;
}

- (void)handleWithSectionDataSoure:(NSMutableArray *)sectionDataSource {
    self.sectionViewModels = sectionDataSource;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.sectionViewModels.count ? self.sectionViewModels.count : self.viewModels.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.viewModels.count > section ? [self.viewModels[section] count]:0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LDSCollectionViewCellViewModel *vModel = [[self.viewModels objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    vModel.collectionView = collectionView;
    vModel.indexPath = indexPath;
    LDSBaseCollectionViewCell *cell = nil;
    cell = (LDSBaseCollectionViewCell *) [vModel cell];
    [cell setModel:vModel.model];
    
    if ([self hasOtherDelegate ]) {
        [self cellAddDelegate:cell];
    }
    return cell;
}

- (void)cellAddDelegate:(id)cell {
    
}

- (BOOL)hasOtherDelegate {
    return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    if ([self.delegate respondsToSelector:@selector(lds_collectionView:didSelectItemAtIndexPath:)]) {
        [self.delegate lds_collectionView:collectionView didSelectItemAtIndexPath:indexPath];
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if ([self.delegate respondsToSelector:@selector(lds_collectionView:layout:insetForSectionAtIndex:)]) {
        return [self.delegate lds_collectionView:collectionView layout:collectionViewLayout insetForSectionAtIndex:section];
    }
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        if (self.sectionViewModels.count <= 0) {
            NSString *identifier = @"HeaderView";
            [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:identifier];
            UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:identifier forIndexPath:indexPath];
            return  headerView;
        }
        LDSCollectionReusableViewModel *vModel = [self.sectionViewModels objectAtIndex:indexPath.section];
        vModel.collectionView = collectionView;
        vModel.indexPath = indexPath;
        LDSBaseCollectionReusableView *reusableView = nil;
        reusableView = (LDSBaseCollectionReusableView *) [vModel reusableView];
        [reusableView setModel:vModel.model];
        reusableview = reusableView;
    }
    
    if (kind == UICollectionElementKindSectionFooter) {
        if (self.sectionViewModels.count <=0) {
            NSString *identifier = @"FooterView";
            [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:identifier];
            UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
            reusableview = footerview;
        }
        
    }
    return reusableview;
}
@end
